package com.gooluke.shorten.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Data;
import org.hibernate.validator.constraints.URL;

import java.util.Date;

/**
 * 短链生成请求参数
 */
@ApiModel(description = "短链生成请求参数")
@Data
public class GenerateRequest {

    @NotBlank(message = "原始URL不能为空")
    @URL(message = "URL格式不正确")
    @Size(max = 2048, message = "URL长度不能超过2048个字符")
    @ApiModelProperty(value = "原始URL", required = true, example = "https://www.example.com/long/path/to/resource")
    private String originalUrl;

    @ApiModelProperty(value = "过期时间（为空表示永不过期）", example = "2025-12-31T23:59:59")
    private Date expireTime;

    @NotNull
    @ApiModelProperty(value = "创建人id", example = "12351235")
    private Long creator;

    @Size(max = 128, message = "域名长度不能超过128个字符")
    @ApiModelProperty(value = "自定义域名（为空使用默认域名）", example = "s.example.com")
    private String domain;

    @NotNull(message = "URL类型不能为空")
    @ApiModelProperty(value = "URL类型：1-系统生成 2-自定义", required = true, example = "1")
    private Integer urlType;

    @Size(min = 4, max = 20, message = "自定义短码长度必须在4-20个字符之间")
    @Pattern(regexp = "^[a-zA-Z0-9_-]+$", message = "自定义短码只能包含字母、数字、下划线和横线")
    @ApiModelProperty(value = "自定义短码（urlType=2时必填）", example = "my-custom-link")
    private String customShortKey;

    /**
     * 验证自定义短码的有效性
     */
    public void validateCustomShortKey() {
        if (urlType == 2 && (customShortKey == null || customShortKey.trim().isEmpty())) {
            throw new IllegalArgumentException("自定义短码不能为空");
        }
    }

}